package com.ruoyi.aliai.controller;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.core.io.InputStreamResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aliai.domain.FileUpload;
import com.ruoyi.aliai.service.IFileUploadService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 微调模型Controller
 * 
 * @author ruoyi
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/upload/upload")
public class FileUploadController extends BaseController
{
    @Resource(name = "customVectorStore")
    private RedisVectorStore vectorStore;
    @Autowired
    private IFileUploadService fileUploadService;

    /**
     * 查询微调模型列表
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileUpload fileUpload)
    {
        startPage();
        List<FileUpload> list = fileUploadService.selectFileUploadList(fileUpload);
        return getDataTable(list);
    }

    /**
     * 导出微调模型列表
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:export')")
    @Log(title = "微调模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileUpload fileUpload)
    {
        List<FileUpload> list = fileUploadService.selectFileUploadList(fileUpload);
        ExcelUtil<FileUpload> util = new ExcelUtil<FileUpload>(FileUpload.class);
        util.exportExcel(response, list, "微调模型数据");
    }

    /**
     * 获取微调模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fileUploadService.selectFileUploadById(id));
    }

    /**
     * 新增微调模型
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:add')")
    @Log(title = "微调模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileUpload fileUpload)
    {
        fileUpload.setUserId(SecurityUtils.getUserId());
        String messages = fileUpload.getMessages();
        if (StringUtils.hasText(messages)) {
            // 如果是纯文本内容，直接创建 Document 对象
            List<Document> splitDocuments = new TokenTextSplitter()
                    .apply(Collections.singletonList(new Document(messages)));
            vectorStore.add(splitDocuments);
        } else {
            throw new IllegalArgumentException("Messages cannot be empty");
        }
        return toAjax(fileUploadService.insertFileUpload(fileUpload));
    }

    /**
     * 修改微调模型
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:edit')")
    @Log(title = "微调模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileUpload fileUpload)
    {
        return toAjax(fileUploadService.updateFileUpload(fileUpload));
    }

    /**
     * 删除微调模型
     */
    @PreAuthorize("@ss.hasPermi('upload:upload:remove')")
    @Log(title = "微调模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fileUploadService.deleteFileUploadByIds(ids));
    }
}
